package com.migo.api;

import com.migo.annotation.IgnoreAuth;
import com.migo.defence.RtCode;
import com.migo.entity.TActivationEntity;
import com.migo.oss.OSSFactory;
import com.migo.service.SysOssService;
import com.migo.service.TActivationService;
import com.migo.utils.DateUtil;
import com.migo.utils.DecCodeUtil;
import com.migo.utils.IpUtils;
import com.migo.utils.R;
import com.migo.validator.Assert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * Created by Wes on 2018/12/25.
 */
@RestController
@RequestMapping("/api")
public class ApiActivationController {


    @Autowired
    OSSFactory ossFactory;

    @Autowired
    private SysOssService sysOssService;

    @Autowired
    private TActivationService tActivationService;


    /**
     * 激活
     */
    @IgnoreAuth
    @RequestMapping(value = "proActivation", produces = "application/json;charset=utf-8;")
    public R proActivation(@RequestParam Map<String, String> params, HttpServletRequest request) {
        String encrypt_str = params.get("encryptStr");
        String userid = params.get("userId");
        String psn = params.get("psn");
        String activation_time = params.get("activationTime");
        try {
            Assert.isBlank(encrypt_str, "encryptStr is null");
            Assert.isBlank(userid, "userId is null");
            Assert.isBlank(psn, "psn is null");
            Assert.isBlank(activation_time, "activationTime is null");
            DecCodeUtil dcu = new DecCodeUtil("pv!#!$16");
            String decrypt = dcu.decrypt(encrypt_str);
            TActivationEntity tActivationEntity = tActivationService.queryObjectForEncrypt(decrypt);
            if(tActivationEntity!=null)
            {
                int status = tActivationEntity.getStatus();
                if (status == 0) {
                    tActivationEntity.setStatus(1);
                    tActivationEntity.setUserId(userid);
                    tActivationEntity.setActivationTime(DateUtil.getNow());
                    if(params.containsKey("lonlat")){
                        tActivationEntity.setLonlat(params.get("lonlat"));
                    }
                    if(params.containsKey("device")){
                        tActivationEntity.setDevicetype(params.get("device"));
                    }
//                    tActivationEntity.setDevicecode(psn);  //更新PSN码
                    tActivationEntity.setUserReqIp(IpUtils.getIpAddr(request));
                    tActivationService.update(tActivationEntity);
                    return R.ok();
                } else {
                    return R.error(RtCode.ActivationTrue.getCode(), RtCode.ActivationTrue.getMsg());
                }
            }
            else {
                return R.error(RtCode.ActivationCodeError.getCode(), RtCode.ActivationCodeError.getMsg());
            }
        } catch (Exception e) {
            return R.error(RtCode.ServerError.getCode(), RtCode.ServerError.getMsg());
        }
    }
}
